import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/authentication_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';

class AuthenticationPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AuthenticationPageController>(
      init: AuthenticationPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '实名认证',
            backgroundColor: pageBgColor,
          ),
          body: GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: ScrollConfigurationCommon(child: ListView(
              children: [
                InputItem(
                  title: '真实姓名', 
                  hintText: '请输入真实姓名',
                  defaultValue: controller.realname,
                  readOnly: controller.status.value == 1 || controller.status.value == 8,
                  onChenge: (String value) {
                    controller.realname = value;
                  }, 
                ),
                InputItem(
                  title: '身份证号', 
                  hintText: '请输入身份证号码', 
                  defaultValue: controller.number.value, 
                  // keyBoardType: TextInputType.number, 
                  readOnly: controller.status.value == 1 || controller.status.value == 8,
                  onChenge: (String value) {
                    controller.number.value = value;
                  }
                ),
                // Obx(() => _imageItem(title: '身份证\n正面图片', style: 1, context: context, path: controller.frontOfIdCardPath.value, controller: controller)),
                // Obx(() => _imageItem(title: '身份证\n反面图片', style: 2, context: context, showBorder: false, path: controller.reverseSideOfIdCardPath.value, controller: controller)),
                controller.status.value == 0 ? Container() : Column(
                  children: [
                    SizedBox(height: 20.h),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 12.w),
                      width: 375.w,
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 12.h),
                        decoration: BoxDecoration(
                          color: bgWhiteColor,
                          borderRadius: BorderRadius.circular(5.w)
                        ),
                        child: _auditStatus(controller.status.value),
                      ),
                    ),
                  ],
                )
              ],
            )),
          ),
          bottomNavigationBar: controller.status.value == 0 || controller.status.value == 7 ? _getBtn(controller) : null
        );
      },
    );
  }

  Widget _getBtn(AuthenticationPageController controller) {
    if (controller.status.value == 0) {
      return BottomAppBar(
        child: Container(
          height: 56.h,
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          alignment: Alignment.center,
          child: ButtonUtil(
            text: '立即认证', 
            fontSize: 16.sp, 
            borderRadius: 30.w, 
            height: 36.h, 
            onTop: () {
              controller.userAuthentication();
            }
          ),
        )
      );
    } else {
      return BottomAppBar(
        child: Container(
          height: 56.h,
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          alignment: Alignment.center,
          child: ButtonUtil(
            text: '重新进行认证', 
            fontSize: 16.sp, 
            borderRadius: 30.w, 
            height: 36.h, 
            onTop: () {
              controller.userAuthentication();
            }
          ),
        )
      );
    }
  }

  // Widget _imageItem({ required String title, required int style, required BuildContext context, required AuthenticationPageController controller, String path = '', bool showBorder = true}) {
  //   return Container(
  //     padding: EdgeInsets.symmetric(horizontal: 12.w),
  //     color: bgWhiteColor,
  //     child: Container(
  //       padding: EdgeInsets.symmetric(horizontal: 4.w),
  //       height: 99.h,
  //       decoration: BoxDecoration(
  //         border: Border(
  //           bottom: showBorder ? BorderSide(
  //             width: .5,
  //             color: lineColor
  //           ) : BorderSide.none
  //         )
  //       ),
  //       child: Row(
  //         children: [
  //           Container(
  //             width: 84.w,
  //             child: Text(
  //               title,
  //               style: TextStyle(
  //                 color: textBlackColor,
  //                 fontSize: 16.sp
  //               ),
  //             ),
  //           ),
  //           InkWellUtil.shakeProofInkWell(path.isEmpty ? Image.asset(
  //             'assets/icons/icon_upload_image.png',
  //             width: 67.w,
  //             height: 67.w,
  //           ) : NetworkImageUtil(
  //             url: path, 
  //             width: 67.w,
  //             height: 67.w,
  //             borderRadius: 5.w,
  //           ), () async {
  //             if (controller.status.value == 1 || controller.status.value == 8) {
  //               return;
  //             }
  //             showSelectPhoto(context, style, controller);
  //           })
  //         ],
  //       ),
  //     ),
  //   );
  // }

  // showSelectPhoto(BuildContext context, int style, AuthenticationPageController controller) {
  //   FocusScope.of(context).requestFocus(FocusNode());
  //   Get.bottomSheet(Container(
  //     height: 168.h + ScreenUtil().bottomBarHeight,
  //     decoration: BoxDecoration(
  //       borderRadius: BorderRadius.vertical(top: Radius.circular(5.w)),
  //       color: bgWhiteColor
  //     ),
  //     child: Column(
  //       children: [
  //         _selectItem(title: '拍照', onTap: () async {
  //           Get.back();
  //           List<UploadImageData> imageArr = await UploadUtil().uploadImageFromCamera(style: style);
  //           if (imageArr.length <= 0) {
  //             return;
  //           }
  //           if (style == 1) {
  //             controller.frontOfIdCard = imageArr[0].id!;
  //             controller.frontOfIdCardPath.value = imageArr[0].image!;
  //           } else {
  //             controller.reverseSideOfIdCard = imageArr[0].id!;
  //             controller.reverseSideOfIdCardPath.value = imageArr[0].image!;
  //           }
  //         }),
  //         _selectItem(title: '相册', onTap: () async {
  //           Get.back();
  //           List<UploadImageData> imagesId = await UploadUtil().uploadImage(context: context, maxAssets: 1, style: style);
  //           if (imagesId.length <= 0) {
  //             return;
  //           }
  //           if (style == 1) {
  //             controller.frontOfIdCard = imagesId[0].id!;
  //             controller.frontOfIdCardPath.value = imagesId[0].image!;
  //           } else {
  //             controller.reverseSideOfIdCard = imagesId[0].id!;
  //             controller.reverseSideOfIdCardPath.value = imagesId[0].image!;
  //           }
  //         }),
  //         _selectItem(title: '取消', isCancel: true, onTap: () {
  //           Get.back();
  //         }),
  //       ],
  //     ),
  //   ));
  // }

  // Widget _selectItem({ required String title, required Function onTap, bool isCancel = false}) {
  //   return InkWellUtil.shakeProofInkWell(Container(
  //     alignment: Alignment.center,
  //     height: 56.h,
  //     decoration: BoxDecoration(
  //       border: Border(
  //         bottom: !isCancel ? BorderSide(
  //           width: .5,
  //           color: lineColor
  //         ) : BorderSide.none
  //       )
  //     ),
  //     child: Text(
  //       title,
  //       style: TextStyle(
  //         color: isCancel ? mainTextColor : textBlackColor,
  //         fontSize: 16.sp
  //       ),
  //     ),
  //   ), () => onTap());
  // }
  
  Widget _auditStatus(int status) {
    /// 默认审核中
    String iconPath = 'assets/icons/icon_under_review.png';
    String title = '认证审核中，请稍等';
    /// 审核通过
    if (status == 8) {
      iconPath = 'assets/icons/icon_approved.png';
      title = '实名认证已通过';
    } 
    /// 审核失败
    if (status == 7) {
      iconPath = 'assets/icons/icon_audit_failed.png';
      title = '认证审核不通过';
    }
    return Column(
      children: [
        Image.asset(
          iconPath,
          width: 44.w,
          height: 44.w,
        ),
        SizedBox(height: 8.h),
        Text(
          title,
          style: TextStyle(
            color: textBlackColor,
            fontSize: 14.sp
          ),
        )
      ],
    );
  }

}